//
//  LXLoginVC.m
//  bgms_p
//
//  Created by xiao.zhao@lachesis-mh.com on 16/2/24.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXLoginVC.h"
#import "LXHowToConnectVC.h"
#import "LXTabBarController.h"
#import "LXSelectSpeakerVC.h"

#import "BLETransDeviceSingleton.h"

@interface LXLoginVC () <BLETransDeviceSingletonDelegate>
{
    BLETransDeviceSingleton *bleCtl;
}
@end

@implementation LXLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nextBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    self.nextBtn.layer.cornerRadius = 5;
    self.nextBtn.layer.masksToBounds = YES;
    
    self.showWarnLabel.numberOfLines = 3;
    self.showWarnLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (_connectSuccess) {
        self.nextBtn.hidden = YES;
        self.discoverLabel.hidden = YES;
        self.showWarnLabel.hidden = YES;
        [self.pairImage setImage:[UIImage imageNamed:@"connectOk"] forState:UIControlStateNormal];
    } else {
        [self bleConnectBegin];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)bleConnectBegin {
    bleCtl = [BLETransDeviceSingleton sharedInstance];
    bleCtl.delegate = self;
    [bleCtl operation];
}

- (IBAction)selectNext:(id)sender {
    LXHowToConnectVC *vc = [[LXHowToConnectVC alloc] initWithNibName:NSStringFromClass([LXHowToConnectVC class]) bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)selectPair:(id)sender {
    [bleCtl scan];
}

- (void)setConnectSuccess:(BOOL)connectSuccess
{
    _connectSuccess = connectSuccess;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        LXTabBarController *vc = [[LXTabBarController alloc] init];
        [[UIApplication sharedApplication].delegate window].rootViewController = vc;
    });
}
#pragma mark -  notification

#pragma mark BLETransDeviceSingletonDelegate
- (void)BLETransDeviceSingletonConnected:(BOOL)connected
{
    if (connected) {
        LXTabBarController *tabBarController = [[LXTabBarController alloc] init];
        UIWindow *window = [[UIApplication sharedApplication].delegate window];
        window.rootViewController = tabBarController;
    } else {
        self.nextBtn.hidden = YES;
        self.discoverLabel.hidden = YES;
        self.showWarnLabel.hidden = NO;
        self.pairImage.enabled = YES;
        self.listenLabel.text = @"Speaker not found";
    }
}

- (void)BLETransDeviceSingletonSetOutput
{
    self.nextBtn.hidden = NO;
    self.discoverLabel.hidden = YES;
    self.showWarnLabel.hidden = YES;
    self.listenLabel.text = @"The BlueTooth is Not Connected";
    self.pairImage.enabled = NO;
}

- (void)BLETransDevice:(BLETransDeviceSingleton *)ble findPeripheral:(NSMutableArray *)peripheralArray
{
//    LXSelectSpeakerVC *vc = [[LXSelectSpeakerVC alloc] initWithNibName:NSStringFromClass([LXSelectSpeakerVC class]) bundle:nil];
//    [self.navigationController pushViewController:vc animated:YES];
//    vc.peripheralArray = peripheralArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
